﻿using LightingServer;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;

namespace ImportMoudel
{
    public class ImportHandler
    {
        public void Import(byte[] bytes)
        {
            try
            {
                LightingServerContext.WriteLine("1 解析模型");
                WorkbookParser parser = new WorkbookParser(bytes);
                WorkbookModel model = parser.Parse();

                LightingServerContext.WriteLine("2 读取Excel");
                SLExcelReader reader = new SLExcelReader();
                SLExcelData excelData = reader.ReadExcel(model.ExcelFile.Stream);

                TableRef[] tableRefs = new TableRef[]
                {
                    new MainTable(),
                    new PhysicalTable(),
                    new OpticsTable(),
                    new ElectricTable(),
                    new AdapterLightingTable(),
                    new ProjectTable(),
                    new BusinessTable(),
                    new PictureTable()
                };

                LightingServerContext.WriteLine("3 打开数据库");
                using (MySqlConnection conn = (MySqlConnection)LightingServerContext.CreateConnection())
                {
                    conn.Open();
                    BuilderCreator builderCreator = new BuilderCreator();
                    PictureCreator pictureCreator = new PictureCreator();
                    int index = 0;
                    LightingServerContext.WriteLine("4 导入数据开始");
                    foreach (List<string> data in excelData.DataRows)
                    {
                        index++;
                        LightingServerContext.WriteLine("");
                        LightingServerContext.WriteLine(string.Format("第【{0}】条数据导入......", index));
                        try
                        {
                            LightingServerContext.WriteLine("主表");
                            Lighting.Builder lightingBuilder = (Lighting.Builder)builderCreator.Create(tableRefs[0], data);
                            if (!lightingBuilder.HasClass)
                            {
                                continue;
                            }

                            LightingServerContext.WriteLine("物理");
                            Physical.Builder physicalBuilder = (Physical.Builder)builderCreator.Create(tableRefs[1], data);
                            lightingBuilder.SetPhysical(physicalBuilder.Build());

                            LightingServerContext.WriteLine("光学");
                            Optics.Builder opticsBuilder = (Optics.Builder)builderCreator.Create(tableRefs[2], data);
                            lightingBuilder.SetOptics(opticsBuilder.Build());

                            LightingServerContext.WriteLine("电气");
                            Electric.Builder electricBuilder = (Electric.Builder)builderCreator.Create(tableRefs[3], data);
                            lightingBuilder.SetElectric(electricBuilder.Build());

                            LightingServerContext.WriteLine("适配");
                            AdapterLighting.Builder adapterLightingBuilder = (AdapterLighting.Builder)builderCreator.Create(tableRefs[4], data);
                            lightingBuilder.SetAdapterLighting(adapterLightingBuilder.Build());

                            LightingServerContext.WriteLine("工程");
                            Project.Builder projectBuilder = (Project.Builder)builderCreator.Create(tableRefs[5], data);
                            lightingBuilder.SetProject(projectBuilder.Build());

                            LightingServerContext.WriteLine("商务");
                            Business.Builder businessBuilder = (Business.Builder)builderCreator.Create(tableRefs[6], data);
                            lightingBuilder.SetBusiness(businessBuilder.Build());

                            LightingServerContext.WriteLine("图标");
                            Picture.Builder pictureBuilder = (Picture.Builder)pictureCreator.Create(
                                tableRefs[7], data, string.Format("{0}|{1}|", excelData.SheetName, index + 2), model);
                            lightingBuilder.SetPicture(pictureBuilder.Build());

                            MySqlTransaction tran = conn.BeginTransaction();
                            LightingCreator creator = new LightingCreator();
                            creator.Create(conn, lightingBuilder.Build());
                            tran.Commit();

                            LightingServerContext.WriteLine(string.Format("第【{0}】条数据导入成功", index));
                        }
                        catch(Exception ex)
                        {
                            LightingServerContext.WriteLine(string.Format("第【{0}】条数据导入......【失败】", index));
                        }
                    }
                   
                    conn.Close();
                    LightingServerContext.WriteLine("5 导入数据结束");
                }
            }
            catch (Exception ex)
            {
                LightingServerContext.WriteLine(ex.StackTrace + ex.Message);
            }
        }
    }
}
